#pragma once

namespace K
{
	template<class K,class V>
	struct BSTreeNode
	{
		BSTreeNode<K,V>* _left;
		BSTreeNode<K,V>* _right;

		K _key;
		V _value;

		BSTreeNode(const K& key,const V& value)
			:_key(key),
			_value(value),
			_left(nullptr),
			_right(nullptr)
		{}
	};

	template <class K,class V>
	class BSTree
	{
		typedef BSTreeNode<K,V> Node;
	public:
		bool insert(const K& key,const V& vlaue)
		{
			if (_root == nullptr)
			{
				_root = new Node(key,vlaue);
				return true;
			}

			Node* cur = _root;
			Node* parent = nullptr;

			while (cur)
			{


				if (cur->_key < key)
				{
					parent = cur;
					cur = cur->_right;
				}
				else if (cur->_key > key)
				{
					parent = cur;
					cur = cur->_left;
				}
				else
				{
					return false;
				}
			}

			cur = new Node(key,vlaue);
			if (parent->_key > key)
			{
				parent->_left = cur;
			}
			else
			{
				parent->_right = cur;
			}

			return true;
		}


		Node* find(const K& key)
		{
			if (_root == nullptr)
			{
				return nullptr;
			}

			Node* cur = _root;
			while (cur)
			{
				if (cur->_key > key)
				{
					cur = cur->_left;
				}
				else if (cur->_key < key)
				{
					cur = cur->_right;
				}
				else
				{
					return cur;
				}
			}

			return nullptr;
		}

		void _InOrder(Node* root)
		{
			if (root == nullptr)
			{
				return;
			}

			_InOrder(root->_left);
			cout << root->_key << " : " << root->_value << endl;
			_InOrder(root->_right);
		}

		void InOrder()
		{
			_InOrder(_root);
		}

		Node* _FindR(Node* root, const K& key)
		{
			if (root == nullptr)
			{
				return nullptr;
			}

			if (root->_key == key)
			{
				return root;
			}

			if (root->_key > key)
			{
				return _FindR(root->_left, key);
			}

			if (root->_key < key)
			{
				return _FindR(root->_right, key);
			}

			return nullptr;
		}



		Node* FindR(const K& key)
		{
			return _FindR(_root, key);
		}

		bool erase(const K& key)
		{
			if (_root == nullptr)
			{
				return false;
			}

			Node* cur = _root;
			Node* parent = nullptr;
			while (cur)
			{

				if (cur->_key > key)
				{
					parent = cur;
					cur = cur->_left;
				}
				else if (cur->_key < key)
				{
					parent = cur;
					cur = cur->_right;
				}
				else
				{
					if (cur->_left == nullptr)
					{
						if (cur == _root)
						{
							_root = _root->_right;
						}
						else
						{

							if (parent->_right == cur)
							{
								parent->_right = cur->_right;
							}
							else
							{
								parent->_left = cur->_right;
							}
						}

						delete cur;
					}
					else if (cur->_right == nullptr)
					{
						//if (cur == _root)
						if (parent == nullptr)
						{
							_root = _root->_left;
						}
						else
						{
							if (parent->_right == cur)
							{
								parent->_right = cur->_left;
							}
							else
							{
								parent->_left = cur->_left;
							}
						}

						delete cur;
					}
					else
					{
						Node* minleft = cur->_right;
						Node* pminleft = cur;
						while (minleft->_left)
						{
							pminleft = minleft;
							minleft = minleft->_left;
						}

						cur->_key = minleft->_key;

						/*if (pminleft->_left == minleft)
						if (pminleft->_left == minleft)
						{
							pminleft->_left = minleft->_right;
						}
						else
						{
							pminleft->_right = minleft->_right;
						}*/


						if (cur->_right == minleft)
						{
							pminleft->_right = minleft->_right;
						}
						else
						{
							pminleft->_left = minleft->_right;
						}
						delete minleft;
					}

					return true;
				}


			}

			return false;
		}

		bool _Erase(Node*& root, const K& key)
		{
			if (root == nullptr)
			{
				return false;
			}

			if (root->_key > key)
			{
				return _Erase(root->_left, key);
			}
			else if (root->_key < key)
			{
				return _Erase(root->_right, key);
			}
			else
			{


				if (root->_left == nullptr)
				{
					Node* del = root;
					root = root->_right;
					delete del;
					return true;
				}
				else if (root->_right == nullptr)
				{
					Node* del = root;
					root = root->_left;
					delete del;
					return true;
				}
				//else
				//{
				//	Node* minright = root->_right;

				//	while (minright->_left)
				//	{
				//		minright = minright->_left;
				//	}

				//	/*swap(root->_key, minright->_key);

				//	return _Erase(root->_right, key);*/

				//	root->_key =  minright->_key;

				//	return _Erase(root->_right, minright->_key);
				//}
				else
				{
					Node* current = root->_right;
					Node* parent = root;
					while (current->_left != nullptr)
					{
						parent = current;
						current = current->_left;
					}
					root->_key = current->_key;
					if (parent->_right == current)
					{
						root->_right = current->_right;
					}
					else
					{
						parent->_left = current->_right;
					}

					delete current;
					return true;
				}


			}
		}

		bool Erase(const K& key)
		{
			return _Erase(_root, key);
		}

		void _Destroy(Node* root)
		{
			if (root == nullptr)
			{
				return;
			}

			_Destroy(root->_left);
			_Destroy(root->_right);

			delete root;
		}

		~BSTree()
		{
			_Destroy(_root);
			_root = nullptr;
		}

		Node* Copy(Node* root)
		{
			if (root == nullptr)
			{
				return nullptr;
			}

			Node* newnode = new Node(root->_key);

			newnode->_left = Copy(root->_left);
			newnode->_right = Copy(root->_right);

			return newnode;
		}

		BSTree(const BSTree<K,V>& t)
		{
			_root = Copy(t._root);
		}

		BSTree() = default;

		BSTree<K,V>& operator=(BSTree<K,V> t)
		{
			swap(_root, t._root);

			return *this;
		}
	private:
		Node* _root = nullptr;
	};
}

namespace KValue
{
	template<class K>
	struct BSTreeNode
	{
		BSTreeNode<K>* _left;
		BSTreeNode<K>* _right;

		K _key;

		BSTreeNode(const K& key)
			:_key(key),
			_left(nullptr),
			_right(nullptr)
		{}
	};

	template <class K>
	class BSTree
	{
		typedef BSTreeNode<K> Node;
	public:
		bool insert(const K& key)
		{
			if (_root == nullptr)
			{
				_root = new Node(key);
				return true;
			}

			Node* cur = _root;
			Node* parent = nullptr;

			while (cur)
			{


				if (cur->_key < key)
				{
					parent = cur;
					cur = cur->_right;
				}
				else if (cur->_key > key)
				{
					parent = cur;
					cur = cur->_left;
				}
				else
				{
					return false;
				}
			}

			cur = new Node(key);
			if (parent->_key > key)
			{
				parent->_left = cur;
			}
			else
			{
				parent->_right = cur;
			}

			return true;
		}

		bool _InsertR(Node*& root, const K& key)
		{
			if (root == nullptr)
			{
				root = new Node(key);
				return true;
			}

			if (root->_key > key)
			{
				return _InsertR(root->_left, key);
			}
			else if (root->_key < key)
			{
				return _InsertR(root->_right, key);
			}
			else
			{
				return false;
			}
		}

		bool InsertR(const K& key)
		{
			return _InsertR(_root, key);
		}

		bool find(const K& key)
		{
			if (_root == nullptr)
			{
				return false;
			}

			Node* cur = _root;
			while (cur)
			{
				if (cur->_key > key)
				{
					cur = cur->_left;
				}
				else if (cur->_key < key)
				{
					cur = cur->_right;
				}
				else
				{
					return true;
				}
			}

			return false;
		}

		void _InOrder(Node* root)
		{
			if (root == nullptr)
			{
				return;
			}

			_InOrder(root->_left);
			cout << root->_key << " ";
			_InOrder(root->_right);
		}

		void InOrder()
		{
			_InOrder(_root);
			cout << endl;
		}

		bool _FindR(Node* root, const K& key)
		{
			if (root == nullptr)
			{
				return false;
			}

			if (root->_key == key)
			{
				return true;
			}

			if (root->_key > key)
			{
				return _FindR(root->_left, key);
			}

			if (root->_key < key)
			{
				return _FindR(root->_right, key);
			}

			return false;
		}



		bool FindR(const K& key)
		{
			return _FindR(_root, key);
		}

		bool erase(const K& key)
		{
			if (_root == nullptr)
			{
				return false;
			}

			Node* cur = _root;
			Node* parent = nullptr;
			while (cur)
			{

				if (cur->_key > key)
				{
					parent = cur;
					cur = cur->_left;
				}
				else if (cur->_key < key)
				{
					parent = cur;
					cur = cur->_right;
				}
				else
				{
					if (cur->_left == nullptr)
					{
						if (cur == _root)
						{
							_root = _root->_right;
						}
						else
						{

							if (parent->_right == cur)
							{
								parent->_right = cur->_right;
							}
							else
							{
								parent->_left = cur->_right;
							}
						}

						delete cur;
					}
					else if (cur->_right == nullptr)
					{
						//if (cur == _root)
						if (parent == nullptr)
						{
							_root = _root->_left;
						}
						else
						{
							if (parent->_right == cur)
							{
								parent->_right = cur->_left;
							}
							else
							{
								parent->_left = cur->_left;
							}
						}

						delete cur;
					}
					else
					{
						Node* minleft = cur->_right;
						Node* pminleft = cur;
						while (minleft->_left)
						{
							pminleft = minleft;
							minleft = minleft->_left;
						}

						cur->_key = minleft->_key;

						/*if (pminleft->_left == minleft)
						if (pminleft->_left == minleft)
						{
							pminleft->_left = minleft->_right;
						}
						else
						{
							pminleft->_right = minleft->_right;
						}*/


						if (cur->_right == minleft)
						{
							pminleft->_right = minleft->_right;
						}
						else
						{
							pminleft->_left = minleft->_right;
						}
						delete minleft;
					}

					return true;
				}


			}

			return false;
		}

		bool _Erase(Node*& root, const K& key)
		{
			if (root == nullptr)
			{
				return false;
			}

			if (root->_key > key)
			{
				return _Erase(root->_left, key);
			}
			else if (root->_key < key)
			{
				return _Erase(root->_right, key);
			}
			else
			{


				if (root->_left == nullptr)
				{
					Node* del = root;
					root = root->_right;
					delete del;
					return true;
				}
				else if (root->_right == nullptr)
				{
					Node* del = root;
					root = root->_left;
					delete del;
					return true;
				}
				//else
				//{
				//	Node* minright = root->_right;

				//	while (minright->_left)
				//	{
				//		minright = minright->_left;
				//	}

				//	/*swap(root->_key, minright->_key);

				//	return _Erase(root->_right, key);*/

				//	root->_key =  minright->_key;

				//	return _Erase(root->_right, minright->_key);
				//}
				else
				{
					Node* current = root->_right;
					Node* parent = root;
					while (current->_left != nullptr)
					{
						parent = current;
						current = current->_left;
					}
					root->_key = current->_key;
					if (parent->_right == current)
					{
						root->_right = current->_right;
					}
					else
					{
						parent->_left = current->_right;
					}

					delete current;
					return true;
				}


			}
		}

		bool Erase(const K& key)
		{
			return _Erase(_root, key);
		}

		void _Destroy(Node* root)
		{
			if (root == nullptr)
			{
				return;
			}

			_Destroy(root->_left);
			_Destroy(root->_right);

			delete root;
		}

		~BSTree()
		{
			_Destroy(_root);
			_root = nullptr;
		}

		Node* Copy(Node* root)
		{
			if (root == nullptr)
			{
				return nullptr;
			}

			Node* newnode = new Node(root->_key);

			newnode->_left = Copy(root->_left);
			newnode->_right = Copy(root->_right);

			return newnode;
		}

		BSTree(const BSTree<K>& t)
		{
			_root = Copy(t._root);
		}

		BSTree() = default;

		BSTree<K>& operator=(BSTree<K> t)
		{
			swap(_root, t._root);

			return *this;
		}
	private:
		Node* _root = nullptr;
	};
}